package org.xx.armory.docs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * 表格元素, 表格元素属于块级文档元素。
 *
 * @author Haart
 */
public class Table
        extends Block
        implements List<TableRow> {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private final List<TableRow> rows;

    /**
     * 构造表格元素。
     *
     * @param className
     *         表格元素的类名, 会被正规化。
     */
    public Table(final String className) {
        super(className);

        rows = new ArrayList<>();
    }

    /**
     * 获取列的个数。
     *
     * @return 列的个数。
     */
    public final int getColumnsCount() {
        if (this.rows.size() == 0) {
            return 1;
        }

        int c = 0;
        final TableRow row = this.rows.get(0);
        for (final TableCell cell : row) {
            c += cell.getColSpan();
        }

        return c;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int size() {
        return this.rows.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean isEmpty() {
        return this.rows.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean contains(final Object o) {
        return this.rows.contains(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Iterator<TableRow> iterator() {
        return this.rows.iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Object[] toArray() {
        return this.rows.toArray();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("SuspiciousToArrayCall")
    public final <T> T[] toArray(final T[] a) {
        return this.rows.toArray(a);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean add(final TableRow e) {
        return this.rows.add(e);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean remove(final Object o) {
        return this.rows.remove(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean containsAll(final Collection<?> c) {
        return this.rows.containsAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean addAll(final Collection<? extends TableRow> c) {
        return this.rows.addAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean addAll(
            final int index,
            final Collection<? extends TableRow> c
    ) {
        return this.rows.addAll(index, c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean removeAll(final Collection<?> c) {
        return this.rows.removeAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean retainAll(Collection<?> c) {
        return this.rows.retainAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void clear() {
        this.rows.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final TableRow get(final int index) {
        return this.rows.get(index);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final TableRow set(
            final int index,
            final TableRow element
    ) {
        return this.rows.set(index, element);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void add(
            final int index,
            final TableRow element
    ) {
        this.rows.add(index, element);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final TableRow remove(final int index) {
        return this.rows.remove(index);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int indexOf(final Object o) {
        return this.rows.indexOf(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int lastIndexOf(final Object o) {
        return this.rows.lastIndexOf(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ListIterator<TableRow> listIterator() {
        return this.rows.listIterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ListIterator<TableRow> listIterator(final int index) {
        return this.rows.listIterator(index);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final List<TableRow> subList(
            final int fromIndex,
            final int toIndex
    ) {
        return this.rows.subList(fromIndex, toIndex);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("<table>\n");
        for (final TableRow row : this.rows) {
            sb.append("  ").append(row).append("\n");
        }
        sb.append("</table>");
        return sb.toString();
    }
}
